package com.administrator.customviewtest.view.materia_design;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.administrator.customviewtest.R;


/**
 * Created by Administrator on 2016/10/28.
 */

public class DialogUtil {

    private static AlertDialog alertDialog;

    private static onBtnClick onBtnClick;

    public static AlertDialog getInstance(Context context, final View view, int leftBtnId, int rightBtnId, final onBtnClick click) {
        if (alertDialog == null) {
            synchronized (DialogUtil.class) {
                if (alertDialog == null) {
                    alertDialog = new AlertDialog.Builder(context).create();
                    onBtnClick = click;

                    view.findViewById(leftBtnId).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            click.onleftBtnClick();
                        }
                    });
                    view.findViewById(rightBtnId).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            click.onleftBtnClick();
                        }
                    });
                    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                        @Override
                        public void onShow(DialogInterface dialog) {
                            alertDialog.getWindow().setContentView(view);
                        }
                    });
                    alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            alertDialog = null;
                        }
                    });
                }
            }
        }
        return alertDialog;
    }

    public static void showDialog(Context context) {
        final View view = LayoutInflater.from(context).inflate(R.layout.layout_dialog, null);
        Dialog dialog = new Dialog(context);
        dialog.setContentView(view);
        dialog.show();
    }


    public static AlertDialog getInstance(Context context, final View view) {
        if (alertDialog == null) {
            synchronized (DialogUtil.class) {
                if (alertDialog == null) {
                    alertDialog = new AlertDialog.Builder(context).setView(view).create();
                    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                        @Override
                        public void onShow(DialogInterface dialog) {

                            alertDialog.getWindow().setLayout(200, 200);
//                            alertDialog.getWindow().setContentView(view);
//                            alertDialog.setView(view,0,0,0,0);
//                            alertDialog.setContentView(view);

                        }
                    });
                    alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            alertDialog = null;
                        }
                    });
                }
            }
        }

        return alertDialog;
    }

    public static AlertDialog getInstance(Context context, CharSequence[] items, DialogInterface.OnClickListener listener) {
        if (alertDialog == null) {
            synchronized (DialogUtil.class) {
                if (alertDialog == null) {
                    alertDialog = new AlertDialog.Builder(context).setItems(items, listener).create();

                    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                        @Override
                        public void onShow(DialogInterface dialog) {
                            alertDialog.getWindow().setLayout(300, 300);
                        }
                    });
                    alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            alertDialog = null;
                        }
                    });
                }
            }
        }

        return alertDialog;
    }


    public interface onBtnClick {
        void onleftBtnClick();
    }

}
